import { create } from "zustand";
import { immer } from "zustand/middleware/immer";

type State = {
  file_ids: number[];
  pdb_ids: string[];
  email: string;
  filter_options: {
    is_filter: boolean;
    bacterial_filter: string[];
    phage_filter: string[];
    prophage_filter: string[];
    options: string[];
  };
  filter_state: {
    Bacteria: boolean;
    Phage: boolean;
    Prophage: boolean;
  };
};

type Actions = {
  pushFileIds: (id: number) => void;
  pushPDBIds: (id: string) => void;
  setFilter: (isFilter: boolean) => void;
  setOptions: (options: string) => void;
  setBacterialFilter: (bacterial: string[]) => void;
  setPhageFilter: (phage: string[]) => void;
  setProPhageFilter: (proPhage: string[]) => void;
  setEmail: (email: string) => void;
  deleteOptions: (options: string) => void;
  clean: () => void;
  setState: (obj: any) => void;
  cleanOption: () => void;
};

export const useCountStore = create<State & Actions>()(
  immer((set) => ({
    file_ids: [],
    pdb_ids: [],
    email: "",
    filter_options: {
      is_filter: true,
      bacterial_filter: [],
      phage_filter: [],
      prophage_filter: [],
      options: [],
    },
    filter_state: {
      Bacteria: false,
      Phage: false,
      Prophage: false,
    },
    setState: (obj: any) =>
      set((state) => {
        state.filter_state = obj;
      }),
    cleanOption: () =>
      set((state) => {
        state.filter_options.options = [];
      }),
    clean: () =>
      set((state) => {
        state.file_ids = [];
        state.pdb_ids = [];
        state.filter_options = {
          is_filter: true,
          bacterial_filter: [],
          phage_filter: [],
          prophage_filter: [],
          options: [],
        };
        state.filter_state = {
          Bacteria: false,
          Phage: false,
          Prophage: false,
        };
        state.email = "";
      }),
    setEmail: (email: string) =>
      set((state) => {
        state.email = email;
      }),
    setFilter: (isFilter: boolean) =>
      set((state) => {
        state.filter_options.is_filter = isFilter;
        if (!isFilter) {
          state.filter_options.bacterial_filter = [];
          state.filter_options.phage_filter = [];
          state.filter_options.prophage_filter = [];
        }
      }),
    setOptions: (options: string) =>
      set((state) => {
        state.filter_options.options.push(options);
      }),
    deleteOptions: (options: string) =>
      set((state) => {
        state.filter_options.options = state.filter_options.options.filter(
          (opt) => opt !== options
        );
      }),
    setBacterialFilter: (bacterial: string[]) =>
      set((state) => {
        state.filter_options.bacterial_filter = bacterial;
      }),
    setPhageFilter: (phage: string[]) =>
      set((state) => {
        state.filter_options.phage_filter = phage;
      }),
    setProPhageFilter: (proPhage: string[]) =>
      set((state) => {
        state.filter_options.prophage_filter = proPhage;
      }),
    pushFileIds: (id: number) =>
      set((state) => {
        state.file_ids.push(id);
      }),
    pushPDBIds: (id: string) =>
      set((state) => {
        state.pdb_ids.push(id);
      }),
  }))
);
